package com.tencent.wuyanzu.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.tencent.wuyanzu.dao.BaseDao;


@Repository
public class BaseDaoImpl implements BaseDao{

	@Autowired
	protected SessionFactory sessionFactory;
	
	@Override
	public Session getSession() {
		return sessionFactory.getCurrentSession();
	}

	@Override
	public Session getNewSession() {
		return sessionFactory.openSession();
	}

	@Override
	public void flush() {
		getSession().flush();
		
	}

	@Override
	public void clear() {
		getSession().clear();
		
	}

	@Override
	public List getListByKeyValue(String key,Object value,Class clazz) {
		Criteria criteria = getSession().createCriteria(clazz);
		criteria.add(Restrictions.eq("sid", value));
		return criteria.list();
	}

	@Override
	public void save(Object bean) {
		try {
			Session session = getNewSession();
			session.save(bean);
			session.flush();
			session.clear();
			session.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

	@Override
	public List getAllValues(Class clazz) {
		Criteria criteria = getSession().createCriteria(clazz);
		return criteria.list();
	}

}
